package com.rxt.config;

import com.rxt.model.property.MinIOConfigProperties;
import io.minio.MinioClient;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Slf4j
@Configuration
public class MinIOConfig {

//    @Autowired
//    private MinIOConfigProperties minIOConfigProperties;

    @Value("${minio.accessKey}")
    private String accessKey;

    @Value("${minio.secretKey}")
    private String secretKey;

    @Value("${minio.bucket}")
    private String bucket;

    @Value("${minio.endpoint}")
    private String endpoint;

    @Value("${minio.readPath}")
    private String readPath;

    @Bean
    public MinIOConfigProperties getMinIOConfigProperties(){
        MinIOConfigProperties minIOConfigProperties = new MinIOConfigProperties();
        minIOConfigProperties.setAccessKey(accessKey);
        minIOConfigProperties.setSecretKey(secretKey);
        minIOConfigProperties.setBucket(bucket);
        minIOConfigProperties.setEndpoint(endpoint);
        minIOConfigProperties.setReadPath(readPath);
        return minIOConfigProperties;
    }

    @Bean
    public MinioClient buildMinioClient(MinIOConfigProperties minIOConfigProperties){
        return MinioClient
                .builder()
                .credentials(minIOConfigProperties.getAccessKey(), minIOConfigProperties.getSecretKey())
                .endpoint(minIOConfigProperties.getEndpoint())
                .build();
    }

}
